//
//  EJSShoppingCarSingleton.m
//  ManShiGuang
//
//  Created by Teresa on 2020/5/14.
//  Copyright © 2020 lifayu. All rights reserved.
//

#import "EJSShoppingCarSingleton.h"
#import "XOShoppingCartListRequest.h"
#import "XOShoppingCartModel.h"


@implementation EJSShoppingCarSingleton

+ (instancetype)sharedSingleton {
    static EJSShoppingCarSingleton *_sharedSingleton = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedSingleton = [[super allocWithZone:NULL] init];
    });
    return _sharedSingleton;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    return [EJSShoppingCarSingleton sharedSingleton];
}

- (id)copyWithZone:(nullable NSZone *)zone {
    return [EJSShoppingCarSingleton sharedSingleton];
}

- (id)mutableCopyWithZone:(nullable NSZone *)zone {
    return [EJSShoppingCarSingleton sharedSingleton];
}

#pragma mark -

-(void)requestShoppingList{
    FlyWeakSelf
       [[XOShoppingCartListRequest new] asyncRequestWithsuccessHandler:^(BaseResponse *response) {
           weakSelf.shoppingList = response.data;
           [[NSNotificationCenter defaultCenter] postNotificationName:Noti_UpdateShoppingCarList object:nil];
           
       } failHandler:^(BaseResponse *response) {
           [[HUDHelper sharedInstance] tipMessage:response.message];
       }];
}

-(void)updateShoppingList:(void (^)(XOShoppingCartListModel * _Nonnull))shoppingList failHandler:(nonnull RequestCompletionHandler)fail{
    FlyWeakSelf
    [[XOShoppingCartListRequest new] asyncRequestWithsuccessHandler:^(BaseResponse *response) {
        weakSelf.shoppingList = response.data;
        shoppingList(response.data);        
    } failHandler:^(BaseResponse *response) {
        fail(response);
    }];
}

-(void)logoutAndEmptyShoppingCar{
//    [self.shoppingList deleteAllGoods];
    self.shoppingList = [XOShoppingCartListModel new];
    [[NSNotificationCenter defaultCenter] postNotificationName:Noti_UpdateShoppingCarList object:nil];
}

@end
